package org.blogwu.learn.multithread.chapter3.demo1;

import java.util.concurrent.locks.ReentrantLock;

public class LockTest {
    public static ReentrantLock lock = new ReentrantLock();
    public static int it = 0;

    public static void main(String[] args) throws InterruptedException {
        RunThread runThread = new RunThread("t1");
        RunThread runThread1 = new RunThread("t2");
        runThread.start();
        runThread1.start();
        runThread.join();
        runThread1.join();
        System.out.println(it);
    }

    public static class RunThread extends Thread {
        public RunThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            for (int i = 0; i < 100000; i++) {
                lock.lock();
                lock.lock();
                try {
                    it++;
                    System.out.println(Thread.currentThread().getName() + ":" + it);
                } finally {
                    lock.unlock();
                    lock.unlock();
                    lock.unlock();
                }
            }
        }
    }
}

